package login;

import login.User.User;
import login.User.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(name = "loginServlet")

public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request获取客户端请求数据的对象
        //response像客户端发送数据（响应）对象
        //设置响应数据的文档类型

        boolean login=true;
        response.setContentType("text/html");
        //设置相应数据的编码
        response.setCharacterEncoding("utf-8");
        //获取打印流
        PrintWriter out=response.getWriter();
        //1.获取客户端数据
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String check=request.getParameter("check");
        String autoLogin=request.getParameter("autoLogin");

        User user= UserDao.login(name, password);
        boolean exit=false;
        if(user!=null){
            exit=true;
        }
        else{
            exit=false;
        }
        if(exit) {
            HttpSession session=request.getSession() ;
            session.setAttribute("username",name);
            String privilege=user.getPrivilege();
            session.setAttribute("privilege",privilege);
            Object lasturl=session.getAttribute("lasturl");
            String url=null;

            if (login) {//如果是第一次登陆
                out.println(name + "恭喜你登陆成功");
                login=false;
                Cookie nameCookie = new Cookie("name", name);
                Cookie passwordCookie = new Cookie("password", password);
                request.getRequestDispatcher("index.jsp").forward(request,response);
                if (check.equals("on")) {
                    nameCookie.setMaxAge(7 * 24 * 3600);
                    passwordCookie.setMaxAge(7 * 24 * 3600);
                    response.addCookie(nameCookie);
                    response.addCookie(passwordCookie);
                }

                //自动登录
                if (autoLogin != null && autoLogin.equals("on")) {
                    Cookie autoLoginCookie = new Cookie("autoLogin", autoLogin);
                    if (check != "on") {
                        nameCookie.setMaxAge(7 * 24 * 3600);
                        passwordCookie.setMaxAge(7 * 24 * 3600);
                        response.addCookie(nameCookie);
                        response.addCookie(passwordCookie);
                    }
                    autoLoginCookie.setMaxAge(7 * 24 * 3600);
                    response.addCookie(autoLoginCookie);
                }

                if(lasturl!=null){
                    url=lasturl.toString();
                }
                if(url!=null&&url.length()>0){
                    response.sendRedirect(url);
                }


            } else {//不是第一次登陆
                if (autoLogin != null && autoLogin.equals("on")) {
                    out.println(name + "恭喜你自动登陆成功");
                }
                else{
                    out.println(name + "恭喜你登陆成功");
                }
            }
        }
        else{
            out.println(name+"对不起，登陆失败");
        }
        out.flush();
        out.close();
    }
}
